# -*- coding: utf-8 -*-
"""
Created on Tue Dec 20 17:52:16 2022

@author: DELL
"""
import jieba
import os
import json
import re
#建立倒排索引
class Index:
    file_names = [] #所有json文件名称
    index_library = {} #索引表
    def __init__(self):
        for file_name in os.listdir('./json'):
            self.file_names.append(file_name)
    def read_json(self, path):
        with open(path,'r') as load_f:
             load_dict = json.load(load_f)
        return load_dict
    def is_chinese(self, uchar):
        if uchar > u'\u4e00' and uchar < u'\u9fa5':
            return True
        else:
            return False
    def is_contains_english(self, ustr):
        my_re = re.compile(r'[A-Za-z]', re.S)
        res = re.findall(my_re, ustr)
        if len(res):
            return True
        else:
            return False
    #要解析的字符串，当前文档的id
    def cut_and_add(self, string, fid):
        if string != '':
            for word in jieba.cut_for_search(string):
                if self.is_chinese(word) or self.is_contains_english(word):
                    if word in self.index_library.keys():
                        #索引表有该词,判断该词是否已经连接到当前文档，若已连接到，出现次数+1
                        if fid in self.index_library[word].keys():
                            self.index_library[word][fid] += 1
                        else:
                            self.index_library[word][fid] = 1
                    else:
                        #索引表中无该词
                        self.index_library[word] = {fid : 1}        
    def build(self):
        for filename in self.file_names:
            doc_dict = self.read_json('./json/' + filename)
            self.cut_and_add(doc_dict['title'], doc_dict['id'])
            if 'good_name' in doc_dict.keys():
                self.cut_and_add(doc_dict['good_name'], doc_dict['id'])
                
            if 'store_name' in doc_dict.keys():
                self.cut_and_add(doc_dict['store_name'], doc_dict['id'])
                
            if 'brand' in doc_dict.keys():
                self.cut_and_add(doc_dict['brand'], doc_dict['id'])
                
            if 'parameter' in doc_dict.keys():
                for p in doc_dict['parameter']:
                    self.cut_and_add(p, doc_dict['id'])
                    
            if 'kinds' in doc_dict.keys():    
                for kind in doc_dict['kinds']:
                    self.cut_and_add(kind, doc_dict['id'])
                    
            if 'detail' in doc_dict.keys():
                for detail in doc_dict['detail']:
                    if 'content' in detail.keys():
                        for content in detail['content']:
        
                            self.cut_and_add(content['dt'], doc_dict['id'])
                            for dd in content['dd']:
                                self.cut_and_add(dd, doc_dict['id'])
            
        
        with open("index_library.json", 'w') as write_f:
            json.dump(self.index_library, write_f, indent=4, ensure_ascii=False)            
        
            


    
    
if __name__ == '__main__':
    index = Index()
    index.build()
    